package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectResultVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjNetworkEmpType;
import com.zb.mapper.ZjNetworkEmpTypeMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjNetworkEmpTypeService;
import com.zb.vo.NetworkTypeListVo;
import com.zb.vo.NetworkTypeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网点人员类型表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-11
 */
@Service
public class ZjNetworkEmpTypeServiceImpl extends ServiceImpl<ZjNetworkEmpTypeMapper, ZjNetworkEmpType> implements ZjNetworkEmpTypeService {
    @Resource
    private ZjNetworkEmpTypeMapper networkEmpTypeMapper;

    // 新增或修改网点人员类型
    @Override
    @Transactional
    public void save(Long custAccId, NetworkTypeVo empTypeVo) {
        Assert.isTrue(null == networkEmpTypeMapper.existByName(custAccId, empTypeVo.getId(), empTypeVo.getName()), "网点人员类型已重复");
        ZjNetworkEmpType type = new ZjNetworkEmpType();
        BeanUtils.copyProperties(empTypeVo, type);
        type.setDisabled(empTypeVo.getStatus());
        if (null != type.getId()) {
            this.updateById(type);
        } else {
            type.setId(IdWorker.getId());
            type.setCustAccId(custAccId);
            this.save(type);
        }
    }

    // 获取网点人员类型列表
    @Override
    public Map<String, Object> getList(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjNetworkEmpType> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjNetworkEmpType> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<NetworkTypeListVo> voList = page.getRecords().stream().map(empType -> {
            NetworkTypeListVo vo = new NetworkTypeListVo();
            BeanUtils.copyProperties(empType, vo);
            vo.setStatus(empType.getDisabled());
            vo.setCreateTime(empType.getGmtCreate());
            vo.setUpdateTime(empType.getGmtUpdate());
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据ID获取网点人员类型详情
    @Override
    public NetworkTypeVo getDetail(Long id) {
        ZjNetworkEmpType empType = this.getById(id);
        Assert.isTrue(null != empType, "网点人员类型参数[ " + id + "]的数据不存在");

        NetworkTypeVo vo = new NetworkTypeVo();
        BeanUtils.copyProperties(empType, vo);
        vo.setStatus(empType.getDisabled());
        return vo;
    }

    // 根据ID批量启用或禁用网点人员类型
    @Override
    public void updateDisableBatchById(List<Long> idList, boolean status) {
        if (null == idList || idList.isEmpty()) {
            return;
        }
        networkEmpTypeMapper.updateDisableBatchById(idList, status);
    }

    // 分页获取网点人员类型下拉数据
    @Override
    public Map<String, Object> networkEmpTypePageSelect(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjNetworkEmpType> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name");
        wrapper.eq("cust_acc_id", custAccId).eq("is_disabled", false);
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjNetworkEmpType> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectResultVo> voList = page.getRecords().stream().map(empType -> {
            return new SelectResultVo(empType.getId(), empType.getName());
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据ID获取分类名称
    @Override
    public List<String> getNameByIds(List<Long> ids) {
        if (null == ids || ids.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkEmpType> wrapper = new QueryWrapper<>();
        wrapper.select("name");
        wrapper.in("id", ids);
        return this.list(wrapper).stream().map(ZjNetworkEmpType::getName).toList();
    }
}
